package com.probiz.estore.product.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.common.model.sales.PromoRule;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.product.dao.ProductSkuDao;

/**
 * Dao implementation for ProductSku.
*/
public class ProductSkuDaoImpl extends HibernateGenericDaoImpl<ProductSku> implements ProductSkuDao {

	@SuppressWarnings("unchecked")
	public List<ProductSku> findActiveProductSkusByProductId(Integer productId) {
		List<ProductSku>productSkus=findByHql("from ProductSku ps where ps.status=1 and ps.product.productId=?",productId);
		return productSkus;
	}
	
	/**
	 * 根据productId 获取默认productSku
	 * @param productSkuId
	 * @return
	 */
	public ProductSku getDefaultProductSkuByProductId(final Integer productId){
		ProductSku sku = (ProductSku)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuffer sb = new StringBuffer();
				sb.append("select p.defaultProductSku from Product p where p.productId = ").append(
						productId);
				Query query = session.createQuery(sb.toString());
				ProductSku row = (ProductSku)query.uniqueResult();
				return row;
			}
		});
		return sku;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.dao.ProductSkuDao#getSizeByProductSkuCodeByNotProductSkuId(java.lang.String, java.lang.Integer)
	 */
	public Long getSizeByProductSkuCodeByNotProductSkuId(final String productSkuCode,final Integer productSkuId){
		Long size;
    	if(productSkuId != null){
    		
    		size = (Long)getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
    			public Object doInHibernate(Session session)
    					throws HibernateException, SQLException {
    				StringBuffer sb = new StringBuffer();
    				sb.append("select count(*) from ProductSku ps where ps.productSkuCode = ? and ps.productSkuId != ?");
    				Query query = session.createQuery(sb.toString());
    				query.setString(0, productSkuCode);
    				query.setInteger(1, productSkuId);
    				Long rows =  (Long)query.uniqueResult();
    				return rows;
    			}

    		});
    	}else{
    		size = (Long)getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
    			public Object doInHibernate(Session session)
    					throws HibernateException, SQLException {
    				StringBuffer sb = new StringBuffer();
    				sb.append("select count(*) from ProductSku ps where ps.productSkuCode = ? ");
    				Query query = session.createQuery(sb.toString());
    				query.setString(0, productSkuCode);
    				Long rows =  (Long)query.uniqueResult();
    				return rows;
    			}

    		});
    	}	
    	return size;
	}
}
